//
//  TeachClassHeadView.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/19.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "TeachClassHeadView.h"

@interface TeachClassHeadView() <FSCalendarDataSource,FSCalendarDelegate,JTCalendarDelegate>{
    
    NSDate *_todayDate;
    NSDate *_minDate;
    NSDate *_maxDate;
    
    NSDate *_dateSelected;
}

@property(nonatomic,strong)NSDate *selectDate;

@end

@implementation TeachClassHeadView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        self.classType = @"团课";
        
        /**
         团课
         */
        UIButton *comClassBtn = [[UIButton alloc] init];
        [comClassBtn setTitle:@"团课"];
        comClassBtn.titleLabel.font = BoldBigFont;
        [comClassBtn setSelectedTitleColor:CSColorFromRGB(0x0a0a0a)];
        [comClassBtn setTitleColor:CSColorFromRGB(0xa5a5a5)];
        comClassBtn.tag = 0;
        comClassBtn.userInteractionEnabled = NO;
        [comClassBtn addTarget:self action:@selector(classBtnClick:)];
        comClassBtn.selected = YES;
        [self addSubview:comClassBtn];
        self.comClassBtn = comClassBtn;
        
        /**
         私教
         */
        UIButton *priClassBtn = [[UIButton alloc] init];
        [priClassBtn setTitle:@"私教"];
        priClassBtn.titleLabel.font = BoldBigFont;
        [priClassBtn setSelectedTitleColor:CSColorFromRGB(0x0a0a0a)];
        [priClassBtn setTitleColor:CSColorFromRGB(0xa5a5a5)];
        priClassBtn.tag = 1;
        priClassBtn.hidden = YES;
        [priClassBtn addTarget:self action:@selector(classBtnClick:)];
        [self addSubview:priClassBtn];
        self.priClassBtn = priClassBtn;
        
        /**
         线条
         */
        UIView *btnLineView = [[UIView alloc] init];
        btnLineView.backgroundColor = ButtonBackGroundColor;
        btnLineView.hidden = YES;
        [self addSubview:btnLineView];
        self.btnLineView = btnLineView;
        
        /**
         显示日期的表格
         */
        UIButton *timeBtn = [[UIButton alloc] init];
        [timeBtn setTitleColor: CSColorFromRGB(0x0a0a0a) forState:UIControlStateNormal];
        [timeBtn setTitleColor: CSColorFromRGB(0x0a0a0a) forState:UIControlStateSelected];
        timeBtn.userInteractionEnabled = NO;
        timeBtn.titleLabel.font = BoldMidBigFont;
        [timeBtn setTitle:[self getCurrentTime] forState:UIControlStateNormal];
        [self addSubview:timeBtn];
        self.timeBtn = timeBtn;
        
        
        //顶部日期选择
        
        self.calendarManager = [JTCalendarManager new];
        self.calendarManager.delegate = self;
        self.calendarManager.settings.weekModeEnabled = YES;
        
        [self createMinAndMaxDate];
        
        JTHorizontalCalendarView *dateCollectionView = [[JTHorizontalCalendarView alloc] init];
        [self addSubview:dateCollectionView];
        self.dateCollectionView = dateCollectionView;
        
        [self.calendarManager setContentView:dateCollectionView];
        [self.calendarManager setDate:_todayDate];
        
        
        /**
         今日按钮
         */
        UIButton *todayBtn = [[UIButton alloc] init];
        [todayBtn setImage:@"icn_today"];
        [todayBtn setHighlightedImage:@"icn_today"];
        [todayBtn addTarget:self action:@selector(todayBtnClick)];
        [self addSubview:todayBtn];
        self.todayBtn = todayBtn;
        
        [self setConstraints];
    }
    
    return self;
}


-(void)setConstraints{
    
    [self.comClassBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(15);
        
        make.left.mas_equalTo(self).offset(12);
    }];
    
    
    [self.priClassBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(15);
        
        make.left.mas_equalTo(self.comClassBtn.mas_right).offset(22);
    }];
    
    [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.comClassBtn);
        
        make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
        
        make.width.mas_equalTo(self.comClassBtn);
        
        make.height.mas_equalTo(1.5);
    }];
    
    
    /**
     显示日期的按钮
     */
    [self.timeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.comClassBtn);
        
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    [self.dateCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(10);
        
        make.right.mas_equalTo(self);
        
        make.left.mas_equalTo(self).offset(SCREEN_WIDTH / 8);
        
        make.height.mas_equalTo(75);
        
        make.bottom.mas_equalTo(self);
    }];
    
    
    [self.todayBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.dateCollectionView);
        
        make.left.mas_equalTo(self).offset(12);
    }];
}


/**
 今日按钮点击
 */
-(void)todayBtnClick{
    
    [_calendarManager setDate:_todayDate];
    
    [self.timeBtn setTitle:[self getCurrentTime]];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    [NotificationCenter postNotificationName:@"selectDate" object:nil userInfo:@{@"selectDate":[formatter stringFromDate:_todayDate]}];
    
    _dateSelected = _todayDate;
    
    [_calendarManager reload];
}

#pragma mark - Fake data

- (void)createMinAndMaxDate
{
    _todayDate = [NSDate date];
    
    // Min date will be 2 month before today
    _minDate = [_calendarManager.dateHelper addToDate:_todayDate months:-2];
    
    // Max date will be 2 month after today
    _maxDate = [_calendarManager.dateHelper addToDate:_todayDate months:2];
}

-(void)layoutSubviews{
    
    [self.timeBtn imageOnTheTitleRightWithSpace:6];

}

-(void)classBtnClick:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    if (tag == 0) {
        
        [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.comClassBtn);
            
            make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
            
            make.width.mas_equalTo(self.comClassBtn);
            
            make.height.mas_equalTo(1.5);
        }];
        
        self.classType = @"团课";
    }
    else{
        
        [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.priClassBtn);
            
            make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
            
            make.width.mas_equalTo(self.comClassBtn);
            
            make.height.mas_equalTo(1.5);
        }];
        
        self.classType = @"私教";
    }
    
    self.comClassBtn.selected = NO;
    
    self.priClassBtn.selected = NO;
    
    sender.selected = !sender.selected;
}


-(void)setClassType:(NSString *)classType{
    
    _classType = classType;
    
    self.comClassBtn.selected = NO;
    
    self.priClassBtn.selected = NO;
    
    if ([classType isEqualToString:@"团课"]) {
        
        [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.comClassBtn);
            
            make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
            
            make.width.mas_equalTo(self.comClassBtn);
            
            make.height.mas_equalTo(1.5);
        }];
        
        self.comClassBtn.selected = YES;
    }
    else{
        
        [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.priClassBtn);
            
            make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
            
            make.width.mas_equalTo(self.comClassBtn);
            
            make.height.mas_equalTo(1.5);
        }];
        
        self.priClassBtn.selected = YES;
    }
}


#pragma mark - CalendarManager delegate

// Exemple of implementation of prepareDayView method
// Used to customize the appearance of dayView
- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView
{
    // Today
//    if([_calendarManager.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]){
////        dayView.circleView.hidden = NO;
////        dayView.circleView.backgroundColor = CSClearColor;
////        dayView.dotView.backgroundColor = [UIColor whiteColor];
////        dayView.textLabel.textColor = [UIColor whiteColor];
//    }
//    // Selected date
//    else
    if(_dateSelected && [_calendarManager.dateHelper date:_dateSelected isTheSameDayThan:dayView.date]){
        dayView.circleView.hidden = NO;
        dayView.circleView.backgroundColor = CSBlackColor;
        dayView.dotView.backgroundColor = [UIColor whiteColor];
        dayView.textLabel.textColor = [UIColor whiteColor];
    }
    // Other month
    else if(![_calendarManager.dateHelper date:_dateCollectionView.date isTheSameMonthThan:dayView.date]){
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor];
        dayView.textLabel.textColor = [UIColor lightGrayColor];
    }
    // Another day of the current month
    else{
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor];
        dayView.textLabel.textColor = [UIColor blackColor];
    }
    
//    if([self haveEventForDay:dayView.date]){
//        dayView.dotView.hidden = NO;
//    }
//    else{
//        dayView.dotView.hidden = YES;
//    }
}

- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView
{
    _dateSelected = dayView.date;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    [NotificationCenter postNotificationName:@"selectDate" object:nil userInfo:@{@"selectDate":[formatter stringFromDate:_dateSelected]}];
    
    [self.timeBtn setTitle:[formatter stringFromDate:_dateSelected]];
    
    // Animation for the circleView
    dayView.circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    [UIView transitionWithView:dayView
                      duration:.3
                       options:0
                    animations:^{
                        dayView.circleView.transform = CGAffineTransformIdentity;
                        [_calendarManager reload];
                    } completion:nil];
    
    
    // Don't change page in week mode because block the selection of days in first and last weeks of the month
    if(_calendarManager.settings.weekModeEnabled){
        return;
    }
    
    // Load the previous or next page if touch a day from another month
    
    if(![_calendarManager.dateHelper date:_dateCollectionView.date isTheSameMonthThan:dayView.date]){
        if([_dateCollectionView.date compare:dayView.date] == NSOrderedAscending){
            [_dateCollectionView loadNextPageWithAnimation];
        }
        else{
            [_dateCollectionView loadPreviousPageWithAnimation];
        }
    }
}

#pragma mark - CalendarManager delegate - Page mangement

// Used to limit the date for the calendar, optional
- (BOOL)calendar:(JTCalendarManager *)calendar canDisplayPageWithDate:(NSDate *)date
{
    return [_calendarManager.dateHelper date:date isEqualOrAfter:_minDate andEqualOrBefore:_maxDate];
}

- (void)calendarDidLoadNextPage:(JTCalendarManager *)calendar
{
    //    NSLog(@"Next page loaded");
}

- (void)calendarDidLoadPreviousPage:(JTCalendarManager *)calendar
{
    //    NSLog(@"Previous page loaded");
}


-(void)setStartDate:(NSString *)startDate{
    
    _startDate = startDate;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    [_calendarManager setDate:[formatter dateFromString:startDate]];
    
    _dateSelected = [formatter dateFromString:startDate];
    
    [_calendarManager reload];
}


#pragma mark 日期视图滑动的通知
-(void)firstDayOfWeekChange:(NSNotification *)sender{
    
    NSDictionary *userInfo = sender.userInfo;
    
    NSDate *date = [userInfo objectForKey:@"firstDayOfWeek"];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"yyyy年-MM月"];
    
    NSString *currentTimeString = [formatter stringFromDate:date];
    
    [self.timeBtn setTitle:currentTimeString];
    
    //[formatter setDateFormat:@"MM月"];
    
    //currentTimeString = [formatter stringFromDate:date];
    
    //self.monthLabel.text = currentTimeString;
}

-(NSString *)getCurrentTime{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 年-MM月
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    return currentTimeString;
}


-(void)dealloc{
    
    self.dateCollectionView = nil;
}
@end
